package com.koron.common.web.log.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.log.entity.po.LogLoginPo;
import com.koron.common.web.log.bean.po.Log;
import com.koron.common.web.log.bean.po.LogQuery;
import com.koron.common.web.log.mapper.LogLoginMapper;
import com.koron.common.web.log.mapper.LogMapper;
import com.koron.common.web.log.service.LogService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 操作日志(Log)表服务实现类
 *
 * @author jzp
 * @since 2022-03-18 08:38:26
 */
@Service
public class LogServiceImpl implements LogService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public Log getById(SessionFactory factory, String id) {
        LogMapper mapper = factory.getMapper(LogMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<Log> list(SessionFactory factory, Log log) {
        LogMapper mapper = factory.getMapper(LogMapper.class);
        return mapper.selectList(log);
    }

    @Override
    @TaskAnnotation("listByBusinessId")
    public List<Log> listByBusinessId(SessionFactory factory, String businessId) {
        Log log = new Log();
        log.setBusinessId(businessId);
        return list(factory, log);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<Log> page(SessionFactory factory, PageQuery pageQuery, LogQuery log) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        LogMapper mapper = factory.getMapper(LogMapper.class);
        List<Log> logList = mapper.page(log);
        return new PageInfo<>(logList);
    }

    @TaskAnnotation("logLoginPage")
    @Override
    public PageInfo<LogLoginPo> logLoginPage(SessionFactory factory, PageQuery pageQuery, LogQuery log) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        LogLoginMapper mapper = factory.getMapper(LogLoginMapper.class);
        List<LogLoginPo> loginPoList = mapper.page(log);
        return new PageInfo<>(loginPoList);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, Log log) {
        LogMapper mapper = factory.getMapper(LogMapper.class);
        return mapper.insertOrUpdate(log) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<Log> logs) {
        if (CollectionUtils.isEmpty(logs)) {
            return false;
        }
        LogMapper mapper = factory.getMapper(LogMapper.class);
        return mapper.insertOrUpdateBatch(logs) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, Log log) {
        LogMapper mapper = factory.getMapper(LogMapper.class);
        return mapper.update(log) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        LogMapper mapper = factory.getMapper(LogMapper.class);
        return mapper.hidden(id) > 0;
    }

    @Override
    @TaskAnnotation("saveLogLogin")
    public Integer saveLogLogin(SessionFactory factory, LogLoginPo loginPo) {
        LogLoginMapper mapper = factory.getMapper(LogLoginMapper.class);
        Integer result = mapper.save(loginPo);
        return result;
    }
}
